package com.example;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;

public class DimenGenerator {

    private static Size BASE;
    private static int ITEM_AMOUNT;
    private static boolean ENABLE_NEGA;
    private static String FILE_NAME;
    private static String NEGATIVE_FILENAME;
    private static String BLANK;
    private static String SIZE_LIST_PATH;
    private static String NORMAL_RES_REG;
    private static String NEGATIVE_RES_REG;
    private static boolean ENABLE_OTHER;
    private static String OTHER_FILE_NAME;
    private static String OTHER_RES_REG;

    public static void main(String[] args) {

        init();

        File destroot = new File("commonsize/src/main/res");

        File listFile = new File("generator" + "/" + SIZE_LIST_PATH);

        ArrayList<Size> sizes = SizeListParser.parseList(listFile);
        for (Size size : sizes) {
            generateFile(destroot, size);
        }
    }

    private static void generateFile(File root, Size size) {
        File dir = new File(root, "values-sw" + size.width + "dp");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File file = new File(dir, FILE_NAME);
        System.out.println(file.getAbsolutePath());
        writeContent(false, false, file, size);
        if (ENABLE_NEGA) {
            file = new File(dir, NEGATIVE_FILENAME);
            writeContent(true, false, file, size);
        }
        if (ENABLE_OTHER) {
            file = new File(dir, OTHER_FILE_NAME);
            writeContent(false, true, file, size);
        }
    }

    private static void writeContent(boolean negative, boolean other, File file, Size size) {
        StringBuilder sb = new StringBuilder();
        sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        sb.append("<resources>").append("\n");
        String reg = negative ? NEGATIVE_RES_REG : NORMAL_RES_REG;
        reg = other ? OTHER_RES_REG : reg;

        //System.out.println(reg);

        sb.append(BLANK)
                .append(reg.replace("{key}", "" + 0).replace("{value}", "" + 0))
                .append("\n");

        // 最关键的步骤，就是算出 参考屏幕上每单位，对应屏幕上多少dp
        float per = (float) size.width / BASE.width;
        
        for (int i = 1; i <= ITEM_AMOUNT; i++) {
            float value = i * per;
            if (negative) {
                sb.append(BLANK)
                        .append(reg.replace("{key}", "" + i).replace("{value}", "-" + value))
                        .append("\n");
            } else {
                sb.append(BLANK)
                        .append(reg.replace("{key}", "" + i).replace("{value}", "" + value))
                        .append("\n");
            }
        }
        sb.append("</resources>");
        try {
            FileWriter writer = new FileWriter(file);
            writer.write(sb.toString());
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String getTimeString() {
        Date nowTime = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
        return dateFormat.format(nowTime);
    }

    /**
     * 初始化
     */
    private static void init() {
        Properties properties = new Properties();
        try {
            File file = new File("generator" + "/" + "config.properties");

            FileInputStream input = new FileInputStream(file);
            properties.load(input);
            input.close();
        } catch (IOException e) {
            //            e.printStackTrace();
        }
        FILE_NAME = properties.getProperty("file_name", "normal_dimens.xml");
        int width = 0;
        int height = 0;
        try {
            width = Integer.parseInt(properties.getProperty("base_width", "720"));
            height = Integer.parseInt(properties.getProperty("base_height", "1280"));
        } catch (NumberFormatException e) {
            //            e.printStackTrace();
        }
        BASE = new Size(width, height);
        System.out.println("base:" + BASE);
        
        ITEM_AMOUNT = Integer.parseInt(properties.getProperty("item_amount", "640"));

        ENABLE_NEGA = Boolean.parseBoolean(properties.getProperty("enable_negative", "true"));
        ENABLE_OTHER = Boolean.parseBoolean(properties.getProperty("enable_other", "true"));
        NEGATIVE_FILENAME = properties.getProperty("negative_file_name", "negative_dimens.xml");
        OTHER_FILE_NAME = properties.getProperty("other_file_name", "other_dimens.xml");
        SIZE_LIST_PATH = properties.getProperty("size_list", "size_list.txt");
        int intent = Integer.parseInt(properties.getProperty("intent", "4"));
        intent = intent < 2 ? 2 : intent;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < intent; i++) {
            sb.append(" ");
        }
        BLANK = sb.toString();
        String temp = "<dimen name=\"{reg}\">{value}dp</dimen>";
        String otherTemp = "<dimen name=\"{reg}\">{value}sp</dimen>";
        NORMAL_RES_REG = properties.getProperty("normal_res_reg", "normal_dimen_{key}");
        NORMAL_RES_REG = temp.replace("{reg}", NORMAL_RES_REG);
        NEGATIVE_RES_REG = properties.getProperty("negative_res_reg", "negative_dimen_{key}");
        NEGATIVE_RES_REG = temp.replace("{reg}", NEGATIVE_RES_REG);
        OTHER_RES_REG = properties.getProperty("other_res_reg", "other_dimen_{key}");
        OTHER_RES_REG = otherTemp.replace("{reg}", OTHER_RES_REG);
    }
}
